﻿Namespace B1Assistant
    Public Class ActiveX
        Public Shared SBOApp As SAPbouiCOM.Application
        Public Shared SBOCompany As SAPbobsCOM.Company

        Shared Sub New()
            If SAPBoAddon.B1Connections.CreateInstance.SBOApp Is Nothing Then
                Throw New Exception("Sbo Application Object is not initialized.")
            End If
            If SAPBoAddon.B1Connections.CreateInstance.SBOApp Is Nothing Then
                Throw New Exception("Sbo Company Object is not initialized.")
            End If
            SBOApp = SAPBoAddon.B1Connections.CreateInstance.SBOApp
            SBOCompany = SAPBoAddon.B1Connections.CreateInstance.SBOCompany
        End Sub

        ''' <summary>
        ''' 添加ActiveX控件
        ''' </summary>
        ''' <param name="uidActiveX"></param>
        ''' <param name="ClassID"></param>
        ''' <param name="oform"></param>
        ''' <param name="uidRectangle">位置控件UID</param>
        ''' <returns>返回控件Item</returns>
        ''' <remarks></remarks>
        Public Shared Function AddActiveX_Item(ByVal uidActiveX As String, ByVal ClassID As String, ByVal oform As SAPbouiCOM.Form, ByVal uidRectangle As String) As SAPbouiCOM.ActiveX
            Dim itmRectangle As SAPbouiCOM.Item
            Dim itmActiveX As SAPbouiCOM.Item
            Dim ActiveX As SAPbouiCOM.ActiveX
            Try
                itmRectangle = oform.Items.Item(uidRectangle)
                itmRectangle.Visible = False
                itmActiveX = oform.Items.Add(uidActiveX, SAPbouiCOM.BoFormItemTypes.it_ACTIVE_X)
                itmActiveX.Top = itmRectangle.Top
                itmActiveX.Left = itmRectangle.Left
                itmActiveX.Width = itmRectangle.Width
                itmActiveX.Height = itmRectangle.Height
                itmActiveX.FromPane = itmRectangle.FromPane
                itmActiveX.ToPane = itmRectangle.ToPane
                itmActiveX.LinkTo = itmRectangle.LinkTo
                itmActiveX.AffectsFormMode = itmRectangle.AffectsFormMode

                ActiveX = itmActiveX.Specific
                ActiveX.ClassID = ClassID
                Return ActiveX
            Catch ex As Exception
                Throw ex
            End Try
        End Function

        ''' <summary>
        ''' 添加ActiveX控件
        ''' </summary>
        ''' <param name="uidActiveX"></param>
        ''' <param name="ClassID"></param>
        ''' <param name="oform"></param>
        ''' <param name="uidRectangle">位置控件UID</param>
        ''' <returns>返回控件对象</returns>
        ''' <remarks></remarks>
        Public Shared Function AddActiveX_Object(ByVal uidActiveX As String, ByVal ClassID As String, ByVal oform As SAPbouiCOM.Form, ByVal uidRectangle As String) As Object
            Dim itmRectangle As SAPbouiCOM.Item
            Dim itmActiveX As SAPbouiCOM.Item
            Dim ActiveX As SAPbouiCOM.ActiveX
            Try
                itmRectangle = oform.Items.Item(uidRectangle)
                itmActiveX = oform.Items.Add(uidActiveX, SAPbouiCOM.BoFormItemTypes.it_ACTIVE_X)
                itmActiveX.Top = itmRectangle.Top
                itmActiveX.Left = itmRectangle.Left
                itmActiveX.Width = itmRectangle.Width
                itmActiveX.Height = itmRectangle.Height
                itmActiveX.FromPane = itmRectangle.FromPane
                itmActiveX.ToPane = itmRectangle.ToPane
                itmActiveX.LinkTo = itmRectangle.LinkTo
                itmActiveX.AffectsFormMode = itmRectangle.AffectsFormMode

                ActiveX = itmActiveX.Specific
                ActiveX.ClassID = ClassID
                Return ActiveX.Object
            Catch ex As Exception
                Throw ex
            End Try
        End Function

    End Class
End Namespace
